#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:robot
@file: cycle.py 
@version:
@time: 2024/01/07 
"""
"""
head = [3, 2, 0, -4]
res = 1
"""


class ListNode:
    def __init__(self, v):
        self.value = v
        self.next = None


t0 = ListNode(3)
t1 = ListNode(2)
t2 = ListNode(0)
t3 = ListNode(-4)
t0.next = t1
t1.next = t2
t2.next = t3
t3.next = t1


def cycle(head: ListNode):
    if head is None: return False
    fast = head
    slow = head
    while fast:
        # 如果快指针走到了链表尾部，即快指针所指向节点的next下一个节点为空，就说明链表无环，终止循环，返回false。
        if fast.next is None:
            return False
        slow = slow.next
        fast = fast.next
        if slow == fast:
            return True
    return False


res = cycle(t0)
print(res)
assert res is True
